/*
 *说明：账号密码信封库存表 首页脚本
 *
 *@author mlc 2012-12-17
*/

var opeForm;
var grid;

$(function(){

	opeForm = $('#opeForm');
	
	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[
		          
		    {title:'节点名称',field:'node_decode',align:'center',width:60},
		    
		    {title:'资源组',field:'rsc_grp_name',align:'center',width:80},
		    
		    {title:'资源名称',field:'rsc_name',align:'center',width:80},
		    
		    {title:'资源账号',field:'rsc_acc',align:'center',width:60},

		    {title:'打印人',field:'print_by_name',align:'center',width:50},

		    {title:'打印时间',field:'print_at',align:'center',width:80},

		    {title:'入库人',field:'instock_by_name',align:'center',width:50},

		    {title:'入库时间',field:'instock_at',align:'center',width:80},

		    {title:'出库人',field:'outstock_by_name',align:'center',width:50},

		    {title:'出库时间',field:'outstock_at',align:'center',width:80},
		    
		    {title:'领用人',field:'used_by_name',align:'center',width:50},

		    {title:'销毁人',field:'destroy_by_name',align:'center',width:60},
 
		    {title:'销毁时间',field:'destroy_at',align:'center',width:80}
        ]],
		pagination:true,
		singleSelect: false
	});	

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#inStorageBtn').click(inStorageOperation);
	$('#outStorageBtn').click(outStorageOperation);
	$('#destroyBtn').click(destroyOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){

	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){

	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'C_ACC_ENV_STOCK!showList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
	
}

//清空操作（重置）
function clearOperation() {

	opeForm.resetForm();
}

//选择资源组
function select_c_rsc_grp() {
	var action = "C_RSC_GRP!showSelect.do?model.rsc_grp_status=01";
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_grp_id').val(row.rsc_grp_id);
		$('#rsc_grp_name').val(row.rsc_grp_name);
	}
}

//选择资源
function select_c_rsc() {
	var action = "C_RSC!showSelect.do?model.rsc_status=01";
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_id').val(row.rsc_id);
		$('#rsc_name').val(row.rsc_name);
	}
}

//查看明细
function showInfo(fid) {

	var href = 'C_ACC_ENV_STOCK!showInfo.do?model.key=' + fid;
	openWin(href,'pop',editWindowWidth,editWindowHeight); 
}

//添加账号密码信封库存表
function addOperation(){  
	var href = 'C_ACC_ENV_STOCK!showAdd.do';
	openWin(href,'pop');
}

//入库
function inStorageOperation() {

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要入库的记录!');	
		return;
	}

	var ids = [];

	for (var i = 0; i < rows.length; i++) {
		if(rows[i].env_status == '02'){
			showInfoWin('您操作的记录已入库，不允许入库操作!');
	      	return; 
		}
		
		if(rows[i].env_status == '03'){
			showInfoWin('您操作的记录已出库，不允许出库操作!');
	      	return; 
		}
		
		if(rows[i].env_status == '04'){
			showInfoWin('您操作的记录已销毁，不允许销毁操作!');
	      	return; 
		}
		ids.push(rows[i].key);
	}

	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要入库选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'C_ACC_ENV_STOCK!doInStorage.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}


//出库
function outStorageOperation(){  

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录!');
      	return; 
	}
	
	if(rows[0].env_status == '01'){
		showInfoWin('您操作的记录未入库，请先入库!');
      	return; 
	}
	
	if(rows[0].env_status == '03'){
		showInfoWin('您操作的记录已出库，不允许出库操作!');
      	return; 
	}
	
	if(rows[0].env_status == '04'){
		showInfoWin('您操作的记录已销毁，不允许出库操作!');
      	return; 
	}

	var href = 'C_ACC_ENV_STOCK!showEdit.do?id=' + rows[0].key;
	openWin(href,'pop');
} 

//销毁
function destroyOperation() {

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要销毁的记录!');	
		return;
	}

	var ids = [];

	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
		if(rows[i].env_status == '01'){
			showInfoWin('您操作的记录未入库，请先入库!');
	      	return; 
		}
		
		if(rows[i].env_status == '02'){
			showInfoWin('您操作的记录未出库，请先出库!');
	      	return; 
		}
		
		if(rows[0].env_status == '04'){
			showInfoWin('您操作的记录已销毁，不允许销毁操作!');
	      	return; 
		}
	}

	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要销毁选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'C_ACC_ENV_STOCK!doDestroy.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//操作成功后刷新列表
function afterOK(){

	queryOperation();
}